package response

import (
	"github.com/gin-gonic/gin"
)

func create(ctx *gin.Context, code int64, items interface{}, msg interface{}) {
	var response = struct {
		Code  int64       `json:"code"`
		Msg   interface{} `json:"msg"`
		Items interface{} `json:"items"`
	}{
		Code:  code,
		Msg:   msg,
		Items: items,
	}
	ctx.JSON(200, response)
	ctx.Abort()
}

func Success(ctx *gin.Context, items interface{}) {
	create(ctx, SUCCESS, items, "")
}

func SuccessNothing(ctx *gin.Context) {
	create(ctx, SUCCESS, nil, "")
}

func SuccessMsg(ctx *gin.Context, msg string) {
	create(ctx, SUCCESS, nil, msg)
}

func SuccessItems2Msg(ctx *gin.Context, items interface{}, msg string) {
	create(ctx, SUCCESS, items, msg)
}

func Fail(ctx *gin.Context, msg string) {
	FailByCode(ctx, FAIL, msg)
}

func FailByCode(ctx *gin.Context, code int64, msg string) {
	create(ctx, code, nil, msg)
}
